package com.kingja.loadsir.core;



import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.callback.SuccessCallback;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.app.Context;

import java.util.List;


public class LoadService<T> {
    private final String TAG = getClass().getSimpleName();
    private LoadLayout loadLayout;
    private Convertor<T> convertor;

    LoadService(Convertor<T> convertor,LoadLayout loadLayout,LoadSir.Builder builder) {
        this.convertor = convertor;
        this.loadLayout = loadLayout;
        initCallback(builder);
    }





    private void initCallback(LoadSir.Builder builder) {
        List<Callback> callbacks = builder.getCallbacks();
        final Class<? extends Callback> defalutCallback = builder.getDefaultCallback();
        if (callbacks != null && callbacks.size() > 0) {
            for (Callback callback : callbacks) {
                loadLayout.setupCallback(callback);
            }
        }
        loadLayout.showCallback(defalutCallback);
    }

    public void showSuccess() {
        loadLayout.showCallback(SuccessCallback.class);
    }

    public void showCallback(Class<? extends Callback> callback) {
        loadLayout.showCallback(callback);
    }

    public void showWithConvertor(T t) {
        if (convertor == null) {
            throw new IllegalArgumentException("You haven't set the Convertor.");
        }
        loadLayout.showCallback(convertor.map(t));
    }

    public LoadLayout getLoadLayout() {
        return loadLayout;
    }

    public Class<? extends Callback> getCurrentCallback() {
        return loadLayout.getCurrentCallback();
    }

    /**
     * obtain rootView if you want keep the toolbar in Fragment
     * @param context Context
     * @param rootView ComponentContainer
     * @param titleView Component
     * @return DirectionalLayout
     */
    public DirectionalLayout getTitleLoadLayout(Context context, ComponentContainer rootView, Component titleView) {
        DirectionalLayout newRootView = new DirectionalLayout(context);
        newRootView.setOrientation(DirectionalLayout.VERTICAL);
        DirectionalLayout.LayoutConfig layoutParams = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,
                DirectionalLayout.LayoutConfig.MATCH_PARENT);
        newRootView.setLayoutConfig(layoutParams);
        rootView.removeComponent(titleView);
        newRootView.addComponent(titleView);
        newRootView.addComponent(loadLayout, layoutParams);
        return newRootView;
    }

    /**
     * modify the callback dynamically
     * @param callback which callback you want modify(layout, event)
     * @param transport a interface include modify logic
     * @return LoadService
     */
    public LoadService<T> setCallBack(Class<? extends Callback> callback, Transport transport) {
        loadLayout.setCallBack(callback, transport);
        return this;
    }
}
